<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col span="5">
					<a-form-item label="委托公司" name="companyCode">
						<CompanySelect v-model:value="searchFormState.companyCode" />
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="所属产品" name="productCode">
						<ProductSelect v-model:value="searchFormState.productCode" />
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="所属项目" name="projectCode">
						<ProjectSelect v-model:value="searchFormState.projectCode" />
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="借款人姓名" name="debtorName">
						<a-input
							v-model:value="searchFormState.debtorName"
							placeholder="请输入借款人姓名"
							enter-button
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="借款人手机号" name="debtorPhone">
						<a-input
							v-model:value="searchFormState.debtorPhone"
							placeholder="请输入借款人手机号"
							enter-button
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="用户标签" name="intentTag">
						<a-select
							v-model:value="searchFormState.intentTag"
							placeholder="请选择用户标签"
							:options="intentTagOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="案件状态" name="caseStatus">
						<a-select
							v-model:value="searchFormState.caseStatus"
							placeholder="请选择案件状态"
							:options="caseStatusOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="甲方主键" name="partnerBusinessId">
						<a-input
							v-model:value="searchFormState.partnerBusinessId"
							placeholder="请输入甲方主键"
							enter-button
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="批次号" name="batchNumber">
						<a-input v-model:value="searchFormState.batchNumber" placeholder="请输入批次号" enter-button allowClear />
					</a-form-item>
				</a-col>
				<a-col span="5">
					<a-form-item label="调解员" name="mediatorName">
						<a-input v-model:value="searchFormState.mediatorName" placeholder="请输入调解员" enter-button allowClear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:scroll="{ x: 'max-content' }"
			:customRow="customRow"
			:rowClassName="(record, index) => (record.isTodayCall === 1 ? 'active-row' : '')"
			:pagination="{ showQuickJumper: true }"
		>
			<template #operator>
				<a-space>
					<a-button v-if="!isMyCase" type="primary" @click="uploadFormRef.openUpload('case')">
						<UploadOutlined />
						案件导入
					</a-button>
					<a-button v-if="!isMyCase" type="primary" @click="uploadFormRef.openUpload('liaison')">
						<UploadOutlined />
						联系人导入
					</a-button>
					<a-button v-if="!isMyCase" type="primary" @click="uploadFormRef.openUpload('excelAllocate')">
						<UploadOutlined />
						批量分案
					</a-button>
					<a-button v-if="!isMyCase" type="primary" @click="caseRetrieval">
						<UploadOutlined />
						案件回收
					</a-button>
					<a-button v-if="!isMyCase" type="primary" @click="userTableRef.onOpen(selectedRowKeys)">
						<UploadOutlined />
						案件分配
					</a-button>
					<a-button v-if="isMyCase" type="primary" @click="exportLiaison">
						<download-outlined />
						联系人导出
					</a-button>
					<!--					<a-button type="primary" @click="uploadFormRef.openUpload('upload')">-->
					<!--						<download-outlined/>-->
					<!--						文件上传-->
					<!--					</a-button>-->
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="toDetail(record)">详情</a>
						<a-divider type="vertical" />
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<uploadForm ref="uploadFormRef" @successful="tableRef.refresh(true)" />
	<userTable ref="userTableRef" @successful="tableRef.refresh(true)" />
</template>

<script setup>
	import caseApi from '@/api/tjzy/case/caseApi'
	import UploadForm from '@/views/tjzy/case/uploadForm.vue'
	import UserTable from '@/views/tjzy/case/userTable.vue'
	import CompanySelect from '@/components/Tjzy/Company/index.vue'
	import ProductSelect from '@/components/Tjzy/Product/index.vue'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import tool from '@/utils/tool'
	import router from '@/router'
	import { Modal } from 'ant-design-vue'
	import { createVNode } from 'vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
	import allocateApi from '@/api/tjzy/allocate/allocateApi'
	import downloadUtil from '@/utils/downloadUtil'

	const props = defineProps({
		qxType: String
	})

	const searchFormState = ref({
		companyCode: undefined,
		productCode: undefined,
		projectCode: undefined,
		debtorName: undefined,
		debtorPhone: undefined,
		partnerBusinessId: undefined,
		intentTag: undefined,
		caseStatus: undefined,
		batchNumber: undefined,
		mediatorName: undefined,
		permissionType: props.qxType,
		sort: 'descending'
	})
	const isMyCase = ref(props.qxType === 'MYSELF')
	const searchFormRef = ref()
	const tableRef = ref()
	const userTableRef = ref()
	const uploadFormRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const intentTagOptions = tool.dictList('IntentTag')
	const caseStatusOptions = tool.dictList('CASE_STATUS')
	const columns = [
		{
			title: '项目名称',
			dataIndex: 'projectName'
		},
		{
			title: '用户标签',
			dataIndex: 'intentTagName'
		},
		{
			title: '借款人姓名',
			dataIndex: 'debtorName'
		},
		{
			title: '借款人手机号',
			dataIndex: 'debtorPhone',
			ellipsis: true
		},
		{
			title: '借款人身份证号',
			dataIndex: 'debtorCardId',
			width: '180px'
		},
		{
			title: '总借款金额',
			dataIndex: 'loanAmount',
			ellipsis: true
		},
		{
			title: '总逾期金额',
			dataIndex: 'overdueAmount'
		},
		{
			title: '应催总金额',
			dataIndex: 'cleanLoanAmount'
		},
		{
			title: '调解公司',
			dataIndex: 'affiliatedCompanyName'
		},
		{
			title: '调解员',
			dataIndex: 'mediatorName'
		},
		{
			title: '案件状态',
			dataIndex: 'caseStatusName'
		},
		{
			title: '批次号',
			dataIndex: 'batchNumber',
			width: '180px'
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: '80px',
			scopedSlots: { customRender: 'action' },
			fixed: 'right'
		}
	]
	let selectedRowKeys = ref([])
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return caseApi.casePage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}

	const caseRetrieval = () => {
		if (selectedRowKeys.value.length === 0) {
			Modal.error({
				title: '提示：',
				okText: '确定',
				content: '尚未选择案件',
				onOk: () => {}
			})
			return
		}
		Modal.confirm({
			title: '提示',
			content: '确认案件回收？',
			icon: createVNode(ExclamationCircleOutlined),
			maskClosable: false,
			onOk() {
				allocateApi.updateCaseMediator({ caseIdList: selectedRowKeys.value }).then((res) => {
					tableRef.value.refresh(true)
					tableRef.value.clearSelected()
				})
			},
			onCancel() {}
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}

	// 自定义行处理器，用于处理行点击等事件
	const customRow = (record, index) => ({
		onDblclick: (event) => {
			toDetail(record)
		}
	})

	const toDetail = (record) => {
		router.push({
			path: '/tjzy/case/detail/' + record.id,
			query: {
				caseId: record.id,
				debtorName: record.debtorName,
				sort: searchFormState.value.sort,
				cleanLoanAmount: record.cleanLoanAmount
			}
		})
	}
	const exportLiaison = () => {
		caseApi.exportMyCaseLiaison(searchFormState.value).then((res) => {
			downloadUtil.resultDownload(res)
		})
	}
</script>
